package com.woniuxy.cloud.carl.marketing.app.apiImpl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.marketing.client.DeductionClient;
import com.woniuxy.cloud.carl.marketing.client.param.AddDeductionParam;
import com.woniuxy.cloud.carl.marketing.client.param.GetCouponParam;
import com.woniuxy.cloud.carl.marketing.infra.model.Coupon;
import com.woniuxy.cloud.carl.marketing.infra.model.CouponSpu;
import com.woniuxy.cloud.carl.marketing.infra.model.Deduction;
import com.woniuxy.cloud.carl.marketing.service.CouponService;
import com.woniuxy.cloud.carl.marketing.service.CouponSpuService;
import com.woniuxy.cloud.carl.marketing.service.DeductionService;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.CommonResultCodeEnum;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class DeductionAPIImpl implements DeductionClient {
    @Autowired
    private DeductionService deductionService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponSpuService couponSpuService;
    @Override
    @Log("添加抵扣券")
    @GlobalTransactional
    public Result addDeductionCoupon(AddDeductionParam param) {
        Result result = deductionService.addDeductionCoupon(param);
        return result;
    }


    @Override
    @Log("查询抵扣优惠券成功")
    public Result<Page<Deduction>> getDeduction(GetCouponParam param) {
        if (param.getPageNum()==null||param.getPageNum()<1)
            param.setPageNum(1);
        if (param.getPageSize()==null||param.getPageSize()<1)
            param.setPageSize(8);
        Result<Page<Deduction>> coupon = deductionService.getCoupon(param);
        return coupon;
    }

    @Override
    public Result<Deduction> useDeduction(Long spuId,Long userId, String couponId) {

        return deductionService.useDeduction(spuId,couponId);
    }
}
